package com.bianguo.android.compresslibrary;

import android.content.Context;
import android.text.TextUtils;

import com.bianguo.android.compresslibrary.bean.PhotoBean;
import com.bianguo.android.compresslibrary.config.CompressConfig;
import com.bianguo.android.compresslibrary.listener.CompressImage;
import com.bianguo.android.compresslibrary.listener.CompressResultListener;
import com.bianguo.android.compresslibrary.utils.CompressUtil;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;

public class CompressImageManager implements CompressImage {

    private final CompressUtil compressUtil;
    private CompressConfig config;
    private ArrayList<PhotoBean> photoBeans;
    private CompressImage.CompressListener listener;

    /**
     * 私有实现
     *
     * @param context 上下文
     * @param config 配置
     * @param photoBeans 图片集合
     * @param listener 监听
     * @return
     */
    private CompressImageManager(Context context, CompressConfig config,
                                 ArrayList<PhotoBean> photoBeans, CompressListener listener) {
        compressUtil = new CompressUtil(context, config);
        this.config = config;
        this.photoBeans = photoBeans;
        this.listener = listener;
    }

    /**
     * 静态方法，new实现
     *
     * @param context 上下文
     * @param config 配置
     * @param photoBeans 图片集合
     * @param listener 监听
     * @return
     */
    public static CompressImage build(Context context, CompressConfig config,
                                      ArrayList<PhotoBean> photoBeans, CompressImage.CompressListener listener) {
        return new CompressImageManager(context, config, photoBeans, listener);
    }

    @Override
    public void compress() {
        if (photoBeans == null || photoBeans.isEmpty()) {
            listener.onCompressFailed(Collections.emptyList(), "图片为空");
            return;
        }
        ArrayList<Integer> errorIndexes = new ArrayList<>();
        int size = photoBeans.size();
        for (int i = 0; i < size; i++) {
            PhotoBean photoBean = photoBeans.get(i);
            if (photoBean == null) {
                errorIndexes.add(i);
            }
            if (!errorIndexes.isEmpty()) {
                listener.onCompressFailed(errorIndexes, "图片为空");
                return;
            }
        }
        int index = 0;
        doCompress(photoBeans.get(index), index, errorIndexes);
    }

    private void doCompress(final PhotoBean photoBean, final int index, final ArrayList<Integer> errorIndexes) {
        String originPath = photoBean.getOriginPath();
        if (index < photoBeans.size()) {
            if (TextUtils.isEmpty(originPath)) {
                errorIndexes.add(index);
                continueCompress(photoBean, false, index, errorIndexes);
                return;
            }
            File file = new File(originPath);
            if (!file.exists() || !file.isFile()) {
                errorIndexes.add(index);
                continueCompress(photoBean, false, index, errorIndexes);
                return;
            }
            if (file.length() < config.getMaxSize()) {
                continueCompress(photoBean, true, index, errorIndexes);
                return;
            }
        }
        compressUtil.compress(originPath, new CompressResultListener() {
            @Override
            public void onCompressSuccess(String imagePath) {
                photoBean.setCompressPath(imagePath);
                int emptyIndex = index + 1;
                if (emptyIndex >= photoBeans.size()) {
                    handlerCallBack(errorIndexes);
                } else {
                    doCompress(photoBeans.get(emptyIndex), emptyIndex, errorIndexes);
                }
            }

            @Override
            public void onCompressFailed(String imagePath, String errorMsg) {
                int emptyIndex = index + 1;
                if (emptyIndex >= photoBeans.size()) {
                    handlerCallBack(errorIndexes, errorMsg);
                } else {
                    doCompress(photoBeans.get(emptyIndex), emptyIndex, errorIndexes);
                }
            }
        });
    }

    private void continueCompress(PhotoBean photoBean, boolean compressed, int index, ArrayList<Integer> errorIndexes) {
        photoBean.setCompressed(compressed);
        index++;
        doCompress(photoBeans.get(index), index, errorIndexes);
    }

    private void handlerCallBack(ArrayList<Integer> errorIndexes, String...error) {
        if (error.length > 0 || !errorIndexes.isEmpty()) {
            listener.onCompressFailed(errorIndexes, error[0]);
            return;
        }
        for (PhotoBean photoBean : photoBeans) {
            if (!photoBean.isCompressed()) {
                listener.onCompressFailed(Collections.emptyList(), "图片压缩失败");
                return;
            }
        }
        listener.onCompressSuccess(photoBeans);
    }
}
